//
//  BXNameAuthenticationVC.m
//  BXlive
//
//  Created by bxlive on 2019/7/26.
//  Copyright © 2019 cat. All rights reserved.
//

#import "BXNameAuthenticationVC.h"
#import "BXGradientButton.h"
#import <TPKeyboardAvoidingScrollView.h>
//#import "CaptureViewController.h"
#import "ZZLActionSheetView.h"
#import <HXPhotoPicker.h>
#import "FilePathHelper.h"
#import "BXBorderButton.h"
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry/Masonry.h>
#import <SDAutoLayout/SDAutoLayout.h>

@interface BXNameAuthenticationVC ()<UIScrollViewDelegate,HXCustomNavigationControllerDelegate,HXCustomCameraViewControllerDelegate,HXPhotoEditViewControllerDelegate>

@property(nonatomic,strong)UIView *navigationView;

@property(nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic, strong) UITextField *nameTf;
@property (nonatomic, strong) UITextField *idTf;
@property (nonatomic, strong) UIButton *idBtn;
@property (nonatomic, assign) BXBorderButton *currentBtn;

@property (strong, nonatomic) HXPhotoManager *manager;
//@property (strong, nonatomic) HXDatePhotoToolManager *toolManager;

@property (nonatomic, copy) NSString *handheldImage;
@property (nonatomic, copy) NSString *frontImage;
@property (nonatomic, copy) NSString *reverseImage;

@property (nonatomic, assign) NSString *card_type;

@end

@implementation BXNameAuthenticationVC
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    };
}
//- (HXDatePhotoToolManager *)toolManager {
//    if (!_toolManager) {
//        _toolManager = [[HXDatePhotoToolManager alloc] init];
//    }
//    return _toolManager;
//}
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
        _manager.configuration.openCamera = YES;
        _manager.configuration.saveSystemAblum = NO;
        _manager.configuration.lookGifPhoto = NO;
        _manager.configuration.selectTogether = NO;
        _manager.configuration.singleSelected = YES;
        _manager.configuration.singleJumpEdit = YES;
        _manager.configuration.movableCropBox = YES;
        _manager.configuration.movableCropBoxEditSize = NO;
        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(500, 500);
        _manager.configuration.photoCanEdit = YES;
        _manager.configuration.videoCanEdit = NO;
        _manager.configuration.rowCount = 4;
        _manager.configuration.themeColor = normalColors;
//        _manager.configuration.restoreNavigationBar = YES;
    }
    return _manager;
}
-(void)backOnClick{
    if (self.isFromChat == YES) {
        [self.navigationController popViewControllerAnimated:YES];
        
    } else {
        
        WS(weakSelf);
        // 是否需要判断返回上一页面还是返回 rootVc
        for (UIViewController *vc in weakSelf.navigationController.viewControllers) {
            if ([vc isKindOfClass:NSClassFromString(@"SLPersonStoreVC")]) {
                [weakSelf popToVc:vc];
                return;
            }
        }
        [self popToRootVc];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = NSLocalizedString(@"实名认证‘", nil);
    self.navigationController.navigationBarHidden = YES;
    self.fd_prefersNavigationBarHidden = YES;
    self.fd_interactivePopDisabled = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    [self setupNavigationView];
    
    _card_type = [self getCardTypeWithIndex:0];
    [self initViews];
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(backOnClick) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"实名认证", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
    
}


- (void)initViews {
    self.scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectZero];
    [self.scrollView setDelegate:self];
    [self.view addSubview:self.scrollView];
    WS(weakSelf);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(weakSelf.navigationView.mas_bottom);
    }];
//    self.scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    if (SCREEN_WIDTH <= __k5Width) {
        self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT+200);
    }else{
        self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT+50);
    }

    UIView *tipView = [UIView new];
    tipView.backgroundColor = LineTitleColor;
    [self.scrollView addSubview:tipView];
    tipView.sd_layout.leftSpaceToView(self.scrollView, 0).rightSpaceToView(self.scrollView, 0).topSpaceToView(self.scrollView, 0).heightIs(35);
   
    tipView.backgroundColor = sl_normalColors;
    UILabel *tipLb = [[UILabel alloc]init];
    tipLb.text = NSLocalizedString(@"为了规范直播环境,请填写以下信息", nil);
    
    tipLb.font = SLPFFont(12);
    tipLb.numberOfLines = 0;
    tipLb.textColor = sl_whiteTextColors;

    [tipView addSubview:tipLb];
    tipLb.sd_layout.leftSpaceToView(tipView, 16).rightSpaceToView(tipView, 16).topSpaceToView(tipView, 0).bottomSpaceToView(tipView, 0);
    
    UILabel *nameLb = [[UILabel alloc]init];
    nameLb.font = SLBFont(16);
    nameLb.text = NSLocalizedString(@"真实姓名", nil);
    nameLb.textColor = sl_textColors;
    [self.scrollView addSubview:nameLb];
    nameLb.sd_layout.leftSpaceToView(self.scrollView, __ScaleWidth(12)).widthIs(85).heightIs(23).topSpaceToView(tipView, __ScaleWidth(40));
   
    
    _nameTf = [[UITextField alloc]init];
    _nameTf.font = SLPFFont(16);
    _nameTf.textColor =sl_textColors;
     _nameTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入您的真实姓名", nil)attributes:@{NSForegroundColorAttributeName:sl_textSubColors}];
    _nameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:_nameTf];
    UIButton *nameTfBtn =[_nameTf valueForKey:@"_clearButton"];
    [nameTfBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    _nameTf.sd_layout.heightIs(40).centerYEqualToView(nameLb).leftSpaceToView(nameLb, 20).rightSpaceToView(self.scrollView, 16);
    
    UIView *nameLineView = [[UIView alloc]init];
    nameLineView.backgroundColor = sl_divideLineColor;
    [self.scrollView addSubview:nameLineView];
    nameLineView.sd_layout.leftEqualToView(_nameTf).rightSpaceToView(self.scrollView, __ScaleWidth(16)).heightIs(1).topSpaceToView(_nameTf, __ScaleWidth(15));
        
//    UILabel *idTypeLabel = [[UILabel alloc]init];
//    idTypeLabel.text = NSLocalizedString(@"证件类型", nil);
//    idTypeLabel.textColor =sl_textColors;
//    idTypeLabel.font = SLBFont(16);
//    [self.scrollView addSubview:idTypeLabel];
//    idTypeLabel.sd_layout.leftEqualToView(nameLb).widthRatioToView(nameLb, 1).heightRatioToView(nameLb, 1).topSpaceToView(nameLineView, __ScaleWidth(30));


//    UIImageView *nextImage = [UIImageView new];
//    nextImage.image = CImage(@"icon_person_nextImage");
//    [self.scrollView addSubview:nextImage];
//    nextImage.sd_layout.rightSpaceToView(self.scrollView, 16).widthIs(7).heightIs(14).centerYEqualToView(idTypeLabel);
//
//    self.idBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.idBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    self.idBtn.titleLabel.font = SLBFont(16);
//    [self.idBtn setTitle:NSLocalizedString(@"身份证", nil) forState:BtnNormal];
//    [self.idBtn setTitleColor:sl_textColors forState:BtnNormal];
//    [self.idBtn addTarget:self action:@selector(idBtnClick) forControlEvents:BtnTouchUpInside];
//    [self.scrollView addSubview:self.idBtn];
//    self.idBtn.sd_layout.leftSpaceToView(idTypeLabel, 20).heightIs(40).rightSpaceToView(self.scrollView, 16).centerYEqualToView(idTypeLabel);
//
//    UIView *idTypeLineView = [[UIView alloc]init];
//    idTypeLineView.backgroundColor = sl_divideLineColor;
//    [self.scrollView addSubview:idTypeLineView];
//    idTypeLineView.sd_layout.leftEqualToView(nameLineView).rightEqualToView(nameLineView).topSpaceToView(self.idBtn, __ScaleWidth(15)).heightIs(1);
//
    UILabel *idLb = [[UILabel alloc]init];
    idLb.text = NSLocalizedString(@"证件号码", nil);
    idLb.textColor = sl_textColors;
    idLb.font = SLBFont(16);
    [self.scrollView addSubview:idLb];
    
    [idLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameLb);
        make.top.mas_equalTo(nameLineView.mas_bottom).mas_offset(12);
        make.width.mas_equalTo(85);
    }];


    _idTf = [[UITextField alloc]init];
    _idTf.font = SLPFFont(16);
    _idTf.textColor =sl_textColors;
    _idTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"请输入您的证件号码", nil) attributes:@{NSForegroundColorAttributeName:sl_textSubColors}];
    _idTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.scrollView addSubview:_idTf];
    UIButton *idBtn =[_idTf valueForKey:@"_clearButton"];
    [idBtn setImage:CImage(@"login_clear") forState:BtnNormal];
    _idTf.sd_layout.leftSpaceToView(idLb, 20).centerYEqualToView(idLb).heightIs(40).rightSpaceToView(self.scrollView, 12);
//
//    UIView *idLineView1 = [[UIView alloc]init];
//     idLineView1.backgroundColor = sl_divideLineColor;
//     [self.scrollView addSubview:idLineView1];
//    idLineView1.sd_layout.leftEqualToView(idTypeLineView).rightEqualToView(id).topSpaceToView(_idTf, __ScaleWidth(15)).heightIs(1);
    
    UIView *idLineView2 = [[UIView alloc]init];
     idLineView2.backgroundColor = sl_subBGColors;
     [self.scrollView addSubview:idLineView2];
    idLineView2.sd_layout.leftSpaceToView(self.scrollView, 0).rightSpaceToView(self.scrollView, 0).topSpaceToView(idLb, __ScaleWidth(15)).heightIs(__ScaleWidth(10));
    
    
    UILabel *handPhotoLb = [[UILabel alloc]init];
    handPhotoLb.text = NSLocalizedString(@"上传手持身份证照片", nil);
    handPhotoLb.textColor =sl_textColors;
    handPhotoLb.font = SLBFont(16);
    [self.scrollView addSubview:handPhotoLb];
    handPhotoLb.sd_layout.leftEqualToView(nameLb).topSpaceToView(idLineView2, __ScaleWidth(34)).heightIs(__ScaleWidth(22)).rightSpaceToView(self.scrollView, __ScaleWidth(12));
    
    CGFloat width = __ScaleWidth(180);
    CGFloat height = __ScaleWidth(112);
    
    BXBorderButton *handPhotoBtn = [[BXBorderButton alloc]init];
    handPhotoBtn.image1.image = [UIImage imageNamed:@"camera"];
    handPhotoBtn.label1.text = @"手持身份证正面合影";
//    [handPhotoBtn setImage:CImage(NSLocalizedString(@"camera", nil)) forState:BtnNormal];
    [handPhotoBtn addTarget:self action:@selector(handPhotoBtnClick:) forControlEvents:BtnTouchUpInside];
    handPhotoBtn.tag = 2;
    [self.scrollView addSubview:handPhotoBtn];
    handPhotoBtn.sd_layout.leftSpaceToView(self.scrollView, __ScaleWidth(12)).topSpaceToView(handPhotoLb, __ScaleWidth(15)).widthIs(width).heightIs(height);
    
    UIImageView *handPhotoImage = [UIImageView new];
    handPhotoImage.image = CImage(NSLocalizedString(@"idcard", nil));
    handPhotoImage.layer.cornerRadius = 12;
    handPhotoImage.layer.masksToBounds = YES;
    [self.scrollView addSubview:handPhotoImage];
    handPhotoImage.sd_layout.rightSpaceToView(self.scrollView, __ScaleWidth(12)).topEqualToView(handPhotoBtn).widthIs(__ScaleWidth(112)).heightIs(__ScaleWidth(112));
    
    
    UILabel *uploadPhotoLb = [[UILabel alloc]init];
    uploadPhotoLb.text = NSLocalizedString(@"上传身份证照片", nil);
    uploadPhotoLb.textColor =sl_textColors;
    uploadPhotoLb.font = SLBFont(16);
    [self.scrollView addSubview:uploadPhotoLb];
    uploadPhotoLb.sd_layout.leftEqualToView(nameLb).topSpaceToView(handPhotoBtn, __ScaleWidth(26)).heightIs(__ScaleWidth(22)).rightSpaceToView(self.scrollView, __ScaleWidth(12));
    
    CGFloat fW = __ScaleWidth(146);
    CGFloat fH = __ScaleHeight(112);
    BXBorderButton *frontBtn = [[BXBorderButton alloc]init];
//    [frontBtn setImage:CImage(NSLocalizedString(@"camera", nil)) forState:BtnNormal];
    frontBtn.image1.image = [UIImage imageNamed:@"camera"];
    frontBtn.label1.text = @"身份证正面照片";
    [frontBtn addTarget:self action:@selector(frontBtnClick:) forControlEvents:BtnTouchUpInside];
    frontBtn.tag = 0;
    [self.scrollView addSubview:frontBtn];
    frontBtn.sd_layout.leftSpaceToView(self.scrollView, __ScaleWidth(12)).topSpaceToView(uploadPhotoLb, __ScaleWidth(15)).widthIs(fW).heightIs(fH);
    
    BXBorderButton *reverseBtn = [[BXBorderButton alloc]init];
//    [reverseBtn setImage:CImage(NSLocalizedString(@"camera", nil)) forState:BtnNormal];
    reverseBtn.image1.image = [UIImage imageNamed:@"camera"];
    reverseBtn.label1.text = @"身份证反面照片";
    [reverseBtn addTarget:self action:@selector(frontBtnClick:) forControlEvents:BtnTouchUpInside];
    reverseBtn.tag = 1;
    [self.scrollView addSubview:reverseBtn];
    reverseBtn.sd_layout.rightSpaceToView(self.scrollView, __ScaleWidth(12)).topSpaceToView(uploadPhotoLb, __ScaleWidth(15)).widthIs(fW).heightIs(fH);
    
    UIButton *nextButton = [[UIButton alloc]init];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"wallat_sure"] forState:UIControlStateNormal];
    nextButton.layer.masksToBounds = YES;
    nextButton.layer.cornerRadius = __ScaleWidth(22);
    [nextButton setTitle:NSLocalizedString(@"提交审核", nil) forState:BtnNormal];
    nextButton.titleLabel.font = SLBFont(16);
    [nextButton addTarget:self action:@selector(submitAction) forControlEvents:BtnTouchUpInside];
    [self.scrollView addSubview:nextButton];
    nextButton.sd_layout.leftSpaceToView(self.scrollView, __ScaleWidth(20)).rightSpaceToView(self.scrollView, __ScaleWidth(20)).heightIs(__ScaleWidth(44)).topSpaceToView(reverseBtn, __ScaleWidth(32));
}

- (NSString *)getCardTypeWithIndex:(NSInteger)index {
    NSString *cardType = nil;
    switch (index) {
        case 0:
            cardType = @"SFZ";
            break;
            
        case 1:
            cardType = @"HZ";
            break;
            
        case 2:
            cardType = @"TXZ";
            break;
            
        case 3:
            cardType = @"TW";
            break;
            
        case 4:
            cardType = @"WAIGUOREN";
            break;
            
        default:
            cardType = @"HKMOJUMIN";
            break;
    }
    return cardType;
}

- (void)addImage:(UIImage *)image {
    _currentBtn.isNoDisplay = YES;
    [_currentBtn setImage:image forState:BtnNormal];
    
    NSInteger index = _currentBtn.tag;
    UIButton *tempBtn = _currentBtn;
    
    NSData *data = UIImagePNGRepresentation(image);
    if (!data) {
        data = UIImageJPEGRepresentation(image, 0.8);
    }
    [SLUpLoadAndDownloadTools uploadFileWithType:@"id_card" fileName:[self getFileName] data:data success:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        if (flag) {
            NSString *url = jsonDic[@"filePath"];
            if (index == 0) {
                self.frontImage = url;
            } else if (index == 1) {
                self.reverseImage = url;
            } else {
                self.handheldImage = url;
            }
        } else {
            [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"上传失败", nil)];
            [tempBtn setImage:nil forState:BtnNormal];
        }
    } failure:^(NSError *error) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"上传失败", nil)];
        [tempBtn setImage:nil forState:BtnNormal];
    }];
}

- (NSString *)getFileName{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置时间格式
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@%d.jpg",str,((arc4random() % 501) + 500)];
    return fileName;
}

#pragma mark 切换身份证等证件
- (void)idBtnClick{
    [self.view endEditing:YES];
    
    WS(ws);
    NSArray *arr = @[NSLocalizedString(@"身份证", nil), NSLocalizedString(@"护照", nil), NSLocalizedString(@"港澳居民来往内地通行证", nil), NSLocalizedString(@"台湾居民来往大陆通行证", nil), NSLocalizedString(@"外国人永久居留身份证", nil), NSLocalizedString(@"港澳台居民居住证", nil)];
    ZZLActionSheetView *action = [[ZZLActionSheetView alloc] initWithTitleView:nil optionsArr:arr cancelTitle:NSLocalizedString(@"取消", nil) cancelBlock:^{
    } selectBlock:^(NSInteger index) {
        [ws.idBtn setTitle:arr[index] forState:BtnNormal];
        ws.card_type = [ws getCardTypeWithIndex:index];
    }];
    [action show];
}
#pragma mark 手持身份证照片
- (void)handPhotoBtnClick:(BXBorderButton *)sender {
    [self.view endEditing:YES];
    _currentBtn = sender;
    
    _manager.configuration.movableCropBoxCustomRatio = CGPointMake(500, 500);
//    [self selectPhoto];
    
    self.manager.type = HXPhotoManagerSelectedTypePhoto;
    
    [self.manager clearSelectedList];
    self.manager.configuration.singleJumpEdit = YES;
    self.manager.configuration.singleSelected = YES;
    WS(weakSelf);
    [self hx_presentSelectPhotoControllerWithManager:weakSelf.manager didDone:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL isOriginal, UIViewController *viewController, HXPhotoManager *manager) {
        
        [photoList hx_requestImageWithOriginal:isOriginal completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
            if (imageArray.count) {
                UIImage *image = imageArray[0];
                [weakSelf addImage:image];
            }
        }];
    } cancel:^(UIViewController *viewController, HXPhotoManager *manager) {
        
    }];
}
#pragma mark 证件照片
- (void)frontBtnClick:(BXBorderButton *)sender {
    [self.view endEditing:YES];
    _currentBtn = sender;
//    if (IsEquallString(self.idBtn.currentTitle, NSLocalizedString(@"身份证", nil))) {
//        WS(ws);
//        CaptureViewController *vc = [[CaptureViewController alloc]init];
//        vc.isBack = sender.tag;
//        [vc setDidGetInfo:^(IDInfo *info, UIImage *image) {
//            [ws addImage:image];
//        }];
//        [self.navigationController pushViewController:vc animated:YES];
//    } else {
        self.manager.configuration.movableCropBoxCustomRatio = CGPointMake(500, 500 * 220 / 334.f);
        self.manager.type = HXPhotoManagerSelectedTypePhoto;
        [self.manager clearSelectedList];
        self.manager.configuration.singleJumpEdit = YES;
        self.manager.configuration.singleSelected = YES;
        WS(weakSelf);
        [self hx_presentSelectPhotoControllerWithManager:weakSelf.manager didDone:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL isOriginal, UIViewController *viewController, HXPhotoManager *manager) {
            
            [photoList hx_requestImageWithOriginal:isOriginal completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
                if (imageArray.count) {
                    UIImage *image = imageArray[0];
                    [weakSelf addImage:image];
                }
            }];
        } cancel:^(UIViewController *viewController, HXPhotoManager *manager) {
            
        }];
//    }
}

- (void)submitAction {
    [self.view endEditing:YES];
    
    if (!_nameTf.text.length) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请输入真实姓名", nil)];
        return;
    }
    if (!_idTf.text.length) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请输入证件号码", nil)];
        return;
    }
    if (!_handheldImage) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请上传手持证件正面合影", nil)];
        return;
    }
    if (!_frontImage) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请上传证件正面照片", nil)];
        return;
    }
    if (!_reverseImage) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"请上传证件反面照片", nil)];
        return;
    }
    
    [BGProgressHUD showLoadingWithMessage:nil];
    [NewHttpManager verificationWithName:_nameTf.text card_num:_idTf.text idcard_type:_card_type hand_idcard:_handheldImage front_idcard:_frontImage back_idcard:_reverseImage is_anchor:_is_anchor success:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        if (flag) {
            NSDictionary *dataDic = jsonDic[@"data"];
            if (dataDic && [dataDic isDictionary]) {
                NSString *verified = dataDic[@"verified"];
                if (verified) {
                    BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
                    liveUser.verified = [NSString stringWithFormat:@"%@",verified];
                    [BXLiveUser setCurrentBXLiveUser:liveUser];
                    
                    if (self.didAuthentication) {
                        self.didAuthentication();
                    }
                    if ([self.is_anchor isEqualToString:@"1"]) {
                        [self popToRootVc];
                    }
                    else{
                        [self.navigationController popViewControllerAnimated:YES];
                    }
                }
            }
        }
        [BGProgressHUD showInfoWithMessage:jsonDic[@"msg"]];
    } failure:^(NSError *error) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"认证失败", nil)];
    }];
}

- (void)selectPhoto{
    [self.manager clearSelectedList];
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"请选择", nil) preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *paizhao = [UIAlertAction actionWithTitle:NSLocalizedString(@"拍照", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self.view hx_showImageHUDText:NSLocalizedString(@"此设备不支持相机!", nil)];
            return;
        }
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"无法使用相机", nil) message:NSLocalizedString(@"请在设置-隐私-相机中允许访问相机", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"设置", nil), nil];
            [alert show];
            
#pragma clang diagnostic pop
            
            return;
        }
        [self hx_presentCustomCameraViewControllerWithManager:self.manager delegate:self];
    }];
    UIAlertAction *xiangce = [UIAlertAction actionWithTitle:NSLocalizedString(@"相册", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [self hx_presentSelectPhotoControllerWithManager:self.manager delegate:self];
#pragma clang diagnostic pop
        
        
    }];
    [alertVc addAction:cancle];
    [alertVc addAction:paizhao];
    [alertVc addAction:xiangce];
    [self presentViewController:alertVc animated:YES completion:nil];
}

#pragma - mark HXCustomCameraViewControllerDelegate
- (void)customCameraViewController:(HXCustomCameraViewController *)viewController didDone:(HXPhotoModel *)model {
    model.selectIndexStr = @"1";
    HXPhotoEditViewController *vc = [[HXPhotoEditViewController alloc] init];
    vc.isInside = YES;
    vc.model = model;
    vc.delegate = self;
    vc.manager = self.manager;
    
    [viewController dismissViewControllerAnimated:NO completion:^{
        [self presentViewController:vc animated:YES completion:nil];
    }];
}

#pragma - mark HXAlbumListViewControllerDelegate
- (void)albumListViewController:(HXAlbumListViewController *)albumListViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
    if (photoList.count) {
        WS(ws);
        HXPhotoModel *model = photoList[0];
        [@[model] hx_requestImageWithOriginal:YES completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
            if (imageArray.count) {
                UIImage *image = imageArray[0];
                [ws addImage:image];
            }
        }];
//        [self.toolManager getSelectedImageList:@[model] success:^(NSArray<UIImage *> *imageList) {
//            if (imageList.count) {
//                UIImage *image = imageList[0];
//                [ws addImage:image];
//            }
//        } failed:^{
//
//        }];
    }
}

#pragma - mark HXPhotoEditViewControllerDelegate
- (void)photoEditViewControllerDidClipClick:(HXPhotoEditViewController *)photoEditViewController beforeModel:(HXPhotoModel *)beforeModel afterModel:(HXPhotoModel *)afterModel {
    WS(ws);
    [@[afterModel] hx_requestImageWithOriginal:YES completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
        if (imageArray.count) {
            UIImage *image = imageArray[0];
            [ws addImage:image];
        }
    }];
    
//    [self.toolManager getSelectedImageList:@[afterModel] success:^(NSArray<UIImage *> *imageList) {
//        if (imageList.count) {
//            UIImage *image = imageList[0];
//            [ws addImage:image];
//        }
//    } failed:^{
//
//    }];
}


@end
